using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace IntegratorComponents.Helpers
{
    public class Encryption
    {
        /// <summary>
        /// String encryptor helper
        /// </summary>
        /// <param name="str"></param>
        /// <param name="scope"></param>
        /// <returns></returns>
        public static string EncryptString(string str, object scope)
        {
            if (str == null)
                return null;

            if (str == "")
                return "";

            MemoryStream fsOut = new MemoryStream();
            SymmetricAlgorithm symm = new RijndaelManaged();
            byte[] key = System.Text.Encoding.ASCII.GetBytes(LicenseKey);
            byte[] key2 = new byte[256 / 8];
            System.Array.Copy(key, key2, Math.Min(key.Length, key2.Length));
            symm.Key = key2;
            symm.IV = new byte[16];
            ICryptoTransform transform = symm.CreateEncryptor();

            CryptoStream cstream = new CryptoStream(fsOut, transform, CryptoStreamMode.Write);
            byte[] chars = System.Text.Encoding.Unicode.GetBytes(str);
            cstream.Write(chars, 0, chars.Length);

            cstream.FlushFinalBlock();
            cstream.Close();

            byte[] encoded = fsOut.ToArray();
            string result = "";

            foreach (byte b in encoded)
            {
                result += b.ToString("d3");
            }

            return "_Encr:" + result;
        }

        /// <summary>
        /// String decryption helper
        /// </summary>
        /// <param name="str"></param>
        /// <param name="scope"></param>
        /// <returns></returns>
        public static string DecryptString(string str, object scope)
        {
            try
            {
                if (string.IsNullOrEmpty(str))
                    return "";

                if (!str.StartsWith("_Encr:"))
                    return str;

                MemoryStream fsIn = new MemoryStream();

                for (int i = 6; i < str.Length; i += 3)
                {
                    fsIn.WriteByte ((byte)int.Parse(str.Substring(i, 3)));
                }

                fsIn.Position = 0;
                MemoryStream fsOut = new MemoryStream();
                SymmetricAlgorithm symm = new RijndaelManaged();
                byte[] key = System.Text.Encoding.ASCII.GetBytes(LicenseKey);
                byte[] key2 = new byte[256 / 8];
                System.Array.Copy(key, key2, Math.Min(key.Length, key2.Length));
                symm.Key = key2;
                symm.IV = new byte[16];
                ICryptoTransform transform = symm.CreateDecryptor();

                CryptoStream cstream = new CryptoStream(fsIn, transform, CryptoStreamMode.Read);

                byte[] buff = new byte[8192];
                int n = cstream.Read(buff, 0, buff.Length);

                while (n > 0)
                {
                    fsOut.Write(buff, 0, n);
                    n = cstream.Read(buff, 0, buff.Length);
                }

                return System.Text.UnicodeEncoding.Unicode.GetString(fsOut.ToArray());
            }
            catch
            {
                return str;
            }
        }

        /// <summary>
        /// License key
        /// </summary>
        private static readonly string LicenseKey = "SSISDEV!seoduefvseh7rv60p[2w>>>>>>!!!!!d!!!!!!";
    }
}
